package com.lebang.controller.api.valid;

import com.lebang.common.MemberLock;
import com.lebang.common.image.VerifyCodeUtils;
import com.lebang.component.RedisClient;
import com.lebang.domain.ValidMemberRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.ValidMemberRecordExample;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.PointService;
import com.lebang.service.sys.ValidMemberRecordService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDate;

/**
 * @Author: hwq
 * @Date: 2020/8/18 11:02
 * @Version 1.0
 */
@Controller
@RequestMapping("/api/valid")
public class ValidApi {
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private ValidMemberRecordService validMemberRecordService;
    @Autowired
    private PointService pointService;
    @PostMapping("/code")
    @ResponseBody
    public JsonData code(HttpServletRequest request) {
        RequestHeaders requestHeaders=new RequestHeaders(request);
            ValidMemberRecordExample example = new ValidMemberRecordExample();
            example.createCriteria().andCreateDateEqualTo(LocalDate.now())
                    .andMemberIdEqualTo(requestHeaders.getUserid())
            .andStatusEqualTo(Whether.NO.getDbIndex());
            example.setOrderByClause("count asc");
            ValidMemberRecord validMemberRecord = validMemberRecordService.selectByMember(requestHeaders.getUserid());
            if(validMemberRecord!=null){
                validMemberRecord.setValidCode("");
                return Callback.success("success", "查询成功", validMemberRecord);
            }else {
                return Callback.fail("success", "查询成功", null);
            }
    }


    @PostMapping("/push")
    @ResponseBody
    @MemberLock
    public JsonData push(HttpServletRequest request,Integer id,String code) {
        RequestHeaders requestHeaders=new RequestHeaders(request);
        ValidMemberRecord record=validMemberRecordService.selectById(id);
        if (record == null) {
            return Callback.fail("200", "任务不存在");
        }
        if(!record.getValidCode().equalsIgnoreCase(code)){
            return Callback.fail("200", "口令错误");
        }
        if (!record.getMemberId().equals(requestHeaders.getUserid())) {
            return Callback.fail("200", "任务不存在");
        }
        if (!record.getStatus().equals(Whether.NO.getDbIndex())) {
            return Callback.fail("200", "任务不存在");
        }
        record.setStatus(Whether.YES.getDbIndex());
        redisClient.delOne("member:code:"+requestHeaders.getUserid());
        validMemberRecordService.updateById(record);
        pointService.addPoint(requestHeaders.getUserid(),record.getPoint(), TaskCategory.KOU_LIN_HONG_BAO,requestHeaders,"口令红包奖励");
        return Callback.success("success", "查询成功", null);
    }

    @GetMapping("/image")
    public void imageCode(HttpServletResponse response,
                          HttpSession session,Integer id) {
        try {
            ValidMemberRecord record=validMemberRecordService.selectById(id);
            if (record == null) {
                return ;
            }
            VerifyCodeUtils.outputImage(100, 32, response.getOutputStream(), record.getValidCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
